<?php

include 'database.php';
include 'template.php';

$doneMessage = "La información ha sido actualizada. <a href=\"Index.php\">Click aquí para volver al inicio</a></h1>";

$mode = htmlspecialchars($_GET["mode"]);

//print_r($_POST);
//print_r(array_keys($_POST));

switch ($mode) {

    case 'index':



        $action = $_POST["categorias"];

        switch ($action) {


            case 'alta':

                //echo "alta";

                $resul = "<form action=\"category.php?mode=alta\" method=\"post\">
            Nombre: <input type=\"text\" name=\"categoryName\" />
            Descripción: <input type=\"text\" name=\"categoryDescription\" />
            <input type=\"submit\" value=\"Guardar\" name=\"guardar\"/>
            </form> ";

                break;

            case 'baja':

                $resul = "<form action=\"category.php?mode=baja\" method=\"post\">
            Nombre: <input type=\"text\" name=\"categoryName\" />
            <input type=\"submit\" value=\"Baja\" name=\"baja\"/>
            </form> ";

                break;

            case 'modificacion':

                $resul = "<form action=\"category.php?mode=modificacion\" method=\"post\">
            Nombre: <input type=\"text\" name=\"categoryName\" />
            Nueva descripción: <input type=\"text\" name=\"categoryDescription\" />
            <input type=\"submit\" value=\"Guardar\" name=\"guardar\"/>
            </form> ";


                break;

            case 'consulta':

                $resul = "<form action=\"category.php?mode=consulta\" method=\"post\">
              
            Nombre: <input type=\"text\" name=\"categoryName\" />
            
            <input type=\"submit\" value=\"Consultar\" name=\"consulta\"/>
            </form> ";

                break;
        }



        break;


    case 'alta':

        //INSERT INTO KrowdixDVE.SNM VALUES (1,0,'YetAnotherSN',1000);

        $query = "INSERT INTO categoria VALUES ('" . $_POST["categoryName"] . "','" . $_POST["categoryDescription"] . "');";
        databaseQuery($query);
        $resul = $doneMessage;

        break;

    case 'modificacion':

        //INSERT INTO KrowdixDVE.SNM VALUES (1,0,'YetAnotherSN',1000);
        //$query = "DELETE FROM categoria WHERE categoriaName='" . $_POST["categoryName"] . "';";
        //databaseQuery($query);
        //$query = "INSERT INTO categoria VALUES ('" . $_POST["categoryName"] . "','" . $_POST["categoryDescription"] . "');";


        $query = "UPDATE categoria SET categoriaDescripcion= '" . $_POST["categoryDescription"] . "' WHERE categoriaName ='" . $_POST["categoryName"] . "';";
        databaseQuery($query);
        $resul = $doneMessage;

        break;


    case 'baja':
        // echo "hacer efectiva la baja";
        $query = "DELETE FROM categoria WHERE categoriaName='" . $_POST["categoryName"] . "';";
        //echo $query;
        databaseQuery($query);
        $resul = $doneMessage;


        break;

    case 'consulta':
        //SELECT * FROM categoria




        $query = "SELECT * FROM categoria WHERE categoriaName LIKE '%" . $_POST["categoryName"] . "%';";



        // $query = "SELECT * FROM categoria;";
        //s$query = "SELECT * FROM categoria WHERE categoriaName='" . $_POST["categoryName"] . "';";

        $resulQuery = databaseQuery($query);
        //Prints out information of that row



        $resul = "";

        while ($row = mysql_fetch_assoc($resulQuery)) {

            $resul = $resul . "<p>Nombre de la categoría: " . $row["categoriaName"] . " | Descripcion: " . $row["categoriaDescripcion"] . "</p>";
        }


        //$resul = "consulta php";
        break;
}

//echo "deberia mostrar resul";
display($resul);
?>
